package com.gdssoft.pdaweb.service.am.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.gdssoft.core.Constants;
import com.gdssoft.core.service.impl.BaseServiceImpl;
import com.gdssoft.core.util.ContextUtil;
import com.gdssoft.core.util.DateUtil;
import com.gdssoft.framework.model.system.AppUser;
import com.gdssoft.framework.service.system.AppUserService;
import com.gdssoft.pdaweb.dao.PdaPlanDetailDao;
import com.gdssoft.pdaweb.dao.am.AmMonthPlanDao;
import com.gdssoft.pdaweb.dao.am.AmMonthPlanReadDao;
import com.gdssoft.pdaweb.dao.am.AmPlanDao;
import com.gdssoft.pdaweb.model.PdaPlanDetail;
import com.gdssoft.pdaweb.model.am.AmMonthPlan;
import com.gdssoft.pdaweb.model.am.AmMonthPlanRead;
import com.gdssoft.pdaweb.model.am.AmPlan;
import com.gdssoft.pdaweb.model.am.PdaAmMeter;
import com.gdssoft.pdaweb.service.PdaPlanDetailService;
import com.gdssoft.pdaweb.service.am.AmPlanService;
import com.gdssoft.pdaweb.service.am.PdaAmMeterListService;
import com.gdssoft.pdaweb.service.am.PdaAmMeterService;
import com.ibm.icu.text.SimpleDateFormat;



public class AmPlanServiceImpl extends BaseServiceImpl<AmPlan>
		implements AmPlanService {
	
	private AmPlanDao dao;
	@Resource
	private PdaPlanDetailService pdaPlanDetailService;
	@Resource
	private PdaAmMeterService pdaAmMeterService;
	@Resource
	private PdaAmMeterListService pdaAmMeterListService;
	@Resource
	private AppUserService appUserService;
	@Resource
	private PdaPlanDetailDao pdaPlanDetailDao;
	@Resource
	private AmMonthPlanDao amMonthPlanDao;
	@Resource
	private AmMonthPlanReadDao amMonthPlanReadDao;

	public AmPlanServiceImpl(AmPlanDao dao) {
		super(dao);
		this.dao = dao;
	}

	@Override
	public List<String> getByPlanId(Long planId) {
		List<PdaPlanDetail> list = pdaPlanDetailService.getByPlanId(planId,PdaPlanDetail.AM);
		List<String> amids = new ArrayList<String>();

		for (PdaPlanDetail ppd : list) {
			amids.add(ppd.getMeterId());
		}
		return amids;
	}

	@Override
	public AmPlan save(AmPlan amPlan, String[] ids,String[] removeds) throws Exception {
		Date now = new Date();
		String un = ContextUtil.getCurrentUser().getUsername();
		
		if (amPlan.getPlanId()!=null) {
			List<PdaPlanDetail> ls = pdaPlanDetailDao.getByPlanId(amPlan.getPlanId(), PdaPlanDetail.AM);
			for (PdaPlanDetail pdaPlanDetail : ls) {
				pdaPlanDetailService.remove(pdaPlanDetail.getId());
			}
		}
		
		try {
			amPlan = super.save(amPlan);
		} catch (Exception e) {
			e.printStackTrace();
		}

		for(String amid : removeds){		//mark as unplanned
			PdaAmMeter wm = pdaAmMeterService.getByAmId(amid);
			wm.setIsPlanned(Constants.UNPLANNED);
			pdaAmMeterService.save(wm);			
		}
		
		for (String amid : ids) {
			PdaPlanDetail pdaPlanDetail = new PdaPlanDetail();
			pdaPlanDetail.setPlanId(amPlan.getPlanId());
			pdaPlanDetail.setMeterId(amid);
			pdaPlanDetail.setType(PdaPlanDetail.AM);
			pdaPlanDetail.setCreateddt(now);
			pdaPlanDetail.setCreatedby(un);
			pdaPlanDetailDao.save(pdaPlanDetail);
			
			PdaAmMeter am = pdaAmMeterService.getByAmId(amid);	//标为已计划
			am.setIsPlanned(Constants.PLANNED);
			pdaAmMeterService.save(am);
		}

		return amPlan;
	}

	@Override
	public boolean execute(long planId, Date beginTime, Date endTime,String username) {
		boolean result = true;

		Date now = new Date();
		String thisMonth = new SimpleDateFormat("yyyy-MM").format(new Date());
		String lastMonth = DateUtil.getLastMonthStr(thisMonth);
		String un = ContextUtil.getCurrentUser().getUsername();

		// 更新状态
		AmPlan amPlan = dao.get(planId);
		amPlan.setPlanStatus((long) 1);
		amPlan.setLastEditBy(un);
		amPlan.setLastEditDt(now);
		//String executor = waterMeterPlan.getExecutor();
		dao.save(amPlan);

		// 写入月计划表
		AmMonthPlan amMonthPlan = new AmMonthPlan();
		amMonthPlan.setBeginTime(beginTime);
		amMonthPlan.setEndTime(endTime);
		amMonthPlan.setMonth(thisMonth);
		amMonthPlan.setPlanId(planId);
		amMonthPlan.setPlanStatus((long) 1);
		amMonthPlan.setCreatedby(un);
		amMonthPlan.setCreateddt(now);
		amMonthPlanDao.save(amMonthPlan);
		amMonthPlan = null;
		amPlan = null;

		PdaAmMeter pdaAmMeter = null;
		List<PdaPlanDetail> list = pdaPlanDetailService.getByPlanId(planId,PdaPlanDetail.AM);
		for (PdaPlanDetail pdaPlanDetail : list) {
			AmMonthPlanRead amMonthPlanRead = new AmMonthPlanRead();
			amMonthPlanRead.setPlanID(planId);
			amMonthPlanRead.setExecutor(username);
			amMonthPlanRead.setCreatedby(un);
			amMonthPlanRead.setCreateddt(now);
			amMonthPlanRead.setMonth(thisMonth);
			pdaAmMeter = pdaAmMeterService.getByAmId(pdaPlanDetail.getMeterId());
			amMonthPlanRead.setAparmentID(pdaAmMeter.getAparmentId());
			amMonthPlanRead.setBuildingID(pdaAmMeter.getBuildingId());
			amMonthPlanRead.setDistrictID(pdaAmMeter.getDistrictId());
			long lastDegree = pdaAmMeterListService.getDegreeByTime(pdaPlanDetail.getMeterId(), lastMonth);
			amMonthPlanRead.setLastDegree(lastDegree);
			amMonthPlanRead.setDegree(0L); // default degree=0
			amMonthPlanRead.setMeterId(pdaAmMeter.getAmId());
			amMonthPlanReadDao.save(amMonthPlanRead);
		}

		return result;
	}

	@Override
	public List<AppUser> getExecutor() {
		return appUserService.findByRoleCode("ROLE_AM_READING");
	}

	@Override
	public List<AmMonthPlanRead> getPlanTask(long planId, String month) {
		List<AmMonthPlanRead> retLs = new ArrayList<AmMonthPlanRead>();
		List<PdaPlanDetail> ls = pdaPlanDetailService.getByPlanId(planId,PdaPlanDetail.AM);

		for (PdaPlanDetail pdaPlanDetail : ls) {
			String meterId = pdaPlanDetail.getMeterId();
			List<AmMonthPlanRead> amMonthPlanReadList = amMonthPlanReadDao.getPlanTask(meterId, month,planId);

			if (amMonthPlanReadList.size() > 0) {
				retLs.add(amMonthPlanReadList.get(0));
			}

		}
		return retLs;
	}

}
